﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import flash.geom.*;
    import Engine.Helpers.*;
    import Display.*;

    public class Pier extends Decoration {

        private const PIER:String = "pier";

        public function Pier(_arg1:String=null){
            super(_arg1);
            setState(STATE_STATIC);
            m_typeName = this.PIER;
            m_isHighlightable = false;
        }
        public function getCommodityNames():Array{
            var _local1:Array = [];
            var _local2:XML = m_item.commodityXml;
            _local1.push(String(_local2.@name));
            return (_local1);
        }
        override public function getToolTipStatus():String{
            var _local2:XML;
            var _local3:String;
            var _local4:String;
            var _local5:int;
            var _local6:int;
            var _local7:int;
            var _local1:String;
            if (((!(Global.isVisiting())) && (!(Global.world.isEditMode)))){
                _local2 = getItem().commodityXml;
                if (_local2){
                    _local3 = _local2.@name;
                    _local4 = ZLoc.t("Main", (("Commodity_" + _local3) + "_friendlyName"));
                    _local5 = Global.player.commodities.getCapacity(_local3);
                    _local6 = Global.player.commodities.getCount(_local3);
                    _local7 = int(_local2.@capacity);
                    _local1 = ((_local4 + "\n") + ZLoc.t("Main", "AddStorage", {amount:_local7}));
                };
            };
            return (_local1);
        }
        public function get waterLength():Number{
            return (m_item.waterLength);
        }
        public function get landSquares():String{
            return (m_item.landSquares);
        }
        public function get dockSquares():String{
            return (m_item.dockSquares);
        }
        public function get freeShipSquares():String{
            return (m_item.freeShipSquares);
        }
        public function get freeShipType():String{
            return (m_item.freeShipType);
        }
        public function get berthSquares():String{
            return (m_item.berthSquares);
        }
        private function checkCollisionWithDockSquare(_arg1:Number, _arg2:Number):Boolean{
            var _local5:String;
            var _local6:Array;
            var _local7:Number;
            var _local8:Number;
            if (m_item.dockSquares == null){
                return (false);
            };
            var _local3:Array = m_item.dockSquares.split(";");
            var _local4:int;
            while (_local4 < _local3.length) {
                _local5 = _local3[_local4];
                _local6 = _local5.split("|");
                _local7 = (this.m_position.x + int(_local6[0]));
                _local8 = (this.m_position.y + int(_local6[1]));
                if ((((_arg1 == _local7)) && ((_arg2 == _local8)))){
                    return (true);
                };
                _local4++;
            };
            return (false);
        }
        public function shipCollidedWithDock(_arg1:Rectangle):Boolean{
            var _local3:int;
            var _local2:int = _arg1.left;
            while (_local2 < (_arg1.left + _arg1.width)) {
                _local3 = _arg1.top;
                while (_local3 < (_arg1.top + _arg1.height)) {
                    if (this.checkCollisionWithDockSquare(_local2, _local3)){
                        return (true);
                    };
                    _local3++;
                };
                _local2++;
            };
            return (false);
        }
        private function checkCollisionWithBerthSquare(_arg1:Number, _arg2:Number):Boolean{
            var _local5:String;
            var _local6:Array;
            var _local7:Number;
            var _local8:Number;
            if (m_item.berthSquares == null){
                return (false);
            };
            var _local3:Array = m_item.berthSquares.split(";");
            var _local4:int;
            while (_local4 < _local3.length) {
                _local5 = _local3[_local4];
                _local6 = _local5.split("|");
                _local7 = (this.m_position.x + int(_local6[0]));
                _local8 = (this.m_position.y + int(_local6[1]));
                if ((((_arg1 == _local7)) && ((_arg2 == _local8)))){
                    return (true);
                };
                _local4++;
            };
            return (false);
        }
        public function isValidShipBerth(_arg1:Rectangle):Boolean{
            var _local3:int;
            var _local2:int = _arg1.left;
            while (_local2 < (_arg1.left + _arg1.width)) {
                _local3 = _arg1.top;
                while (_local3 < (_arg1.top + _arg1.height)) {
                    if (this.checkCollisionWithBerthSquare(_local2, _local3)){
                        return (true);
                    };
                    _local3++;
                };
                _local2++;
            };
            return (false);
        }
        private function getBerthSquareLayer(_arg1:Number, _arg2:Number):int{
            var _local5:String;
            var _local6:Array;
            var _local7:Number;
            var _local8:Number;
            if (m_item.berthSquares == null){
                return (-1);
            };
            var _local3:Array = m_item.berthSquares.split(";");
            var _local4:int;
            while (_local4 < _local3.length) {
                _local5 = _local3[_local4];
                _local6 = _local5.split("|");
                _local7 = (this.m_position.x + int(_local6[0]));
                _local8 = (this.m_position.y + int(_local6[1]));
                if ((((_arg1 == _local7)) && ((_arg2 == _local8)))){
                    return (int(_local6[2]));
                };
                _local4++;
            };
            return (-1);
        }
        public function getShipLayer(_arg1:Rectangle):int{
            var _local5:int;
            var _local2 = -1;
            var _local3 = -1;
            var _local4:int = _arg1.left;
            while (_local4 < (_arg1.left + _arg1.width)) {
                _local5 = _arg1.top;
                while (_local5 < (_arg1.top + _arg1.height)) {
                    _local3 = this.getBerthSquareLayer(_local4, _local5);
                    if (_local3 == 1){
                        return (1);
                    };
                    if (_local3 == 0){
                        return (0);
                    };
                    _local5++;
                };
                _local4++;
            };
            if (_local3 == -1){
                return (1);
            };
            return (_local2);
        }
        override public function sell():void{
            if (m_item){
                if (m_item.cost <= 1){
                    super.sellNow();
                } else {
                    UI.displayMessage(ZLoc.t("Main", "SellPierSpecific", {coins:getSellPrice()}), GenericPopup.TYPE_YESNO, sellConfirmationHandler);
                };
            } else {
                super.sell();
            };
        }
        override public function onSell():void{
            super.onSell();
            this.sellAnyBerthedShips();
        }
        private function sellAnyBerthedShips():void{
            var _local3:Ship;
            var _local4:Ship;
            var _local1:Array = Global.world.getObjectsByClass(Ship);
            var _local2:Array = new Array();
            for each (_local3 in _local1) {
                if (!_local3.isValidShipPosition()){
                    _local2.push(_local3);
                };
            };
            for each (_local4 in _local2) {
                _local4.forceSell();
            };
        }
        override public function onObjectDrop(_arg1:Vector3):void{
            super.onObjectDrop(_arg1);
            this.moveAnyStrayShips(_arg1);
        }
        public function moveAnyStrayShips(_arg1:Vector3):void{
            var _local5:Ship;
            var _local6:Ship;
            var _local2:Vector3 = new Vector3((_arg1.x - this.m_position.x), (_arg1.y - this.m_position.y), (_arg1.z - this.m_position.z));
            var _local3:Array = Global.world.getObjectsByClass(Ship);
            var _local4:Array = new Array();
            for each (_local5 in _local3) {
                if (!_local5.isValidShipPosition()){
                    _local4.push(_local5);
                };
            };
            for each (_local6 in _local4) {
                _local6.forceMove(_local2);
            };
        }
        private function placeAnyFreeShips():void{
            var _local4:String;
            var _local5:Array;
            var _local6:Number;
            var _local7:Number;
            var _local8:Number;
            var _local1:Array = m_item.freeShipSquares.split(";");
            var _local2:String = m_item.freeShipType;
            var _local3:int;
            while (_local3 < _local1.length) {
                _local4 = _local1[_local3];
                _local5 = _local4.split("|");
                _local6 = (this.m_position.x + int(_local5[0]));
                _local7 = (this.m_position.y + int(_local5[1]));
                _local8 = int(_local5[2]);
                Ship.placeFreeShip(_local2, _local6, _local7, _local8);
                _local3++;
            };
        }
        override public function onBuildingConstructionCompleted_PostServerUpdate():void{
            super.onBuildingConstructionCompleted_PostServerUpdate();
            this.placeAnyFreeShips();
        }
        override public function get isMoveLocked():Boolean{
            var _local2:Ship;
            var _local1:Array = Global.world.getObjectsByClass(Ship);
            for each (_local2 in _local1) {
                if (_local2.connectedToPier(this)){
                    return (true);
                };
            };
            return (false);
        }
        override public function isPlacedObjectNonBuilding():Boolean{
            return (false);
        }
        override public function GetNoMoveMessage():String{
            return (ZLoc.t("Main", "NoMovePier"));
        }

    }
}//package Classes 
